package net.cl.db.core;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.cl.db.DBException;
import net.cl.db.DBExecutor;
import net.cl.db.sql.SqlBuilder;

public class BaseDao {

	private DBExecutor db;
	private static final String TABLE_NAME_SYMBOL = "@tableName";

	public BaseDao(DBExecutor db) {
		this.db = db;
	}

	public void save(Object bean) {
		Class<?> sourceType = bean.getClass();
		// Field f = sourceType.getField("tableName");
	}

	public Object get(String tableName, String column, Serializable value) throws DBException {
		SqlBuilder sql = new SqlBuilder(db.getDialect().selectSql());
		sql.append(tableName).eq(column, value);
		System.out.println(DBTools.getDumpSql(db.query(sql)));
		return null;
	}

	public Object get(Map<String, Object> table) throws DBException {
		if (table.get(TABLE_NAME_SYMBOL) == null)
			throw new DBException("the parameter must contains the key '" + TABLE_NAME_SYMBOL + "'");
		SqlBuilder sql = new SqlBuilder(db.getDialect().selectSql());
		sql.append(table.get(TABLE_NAME_SYMBOL));
		for (Iterator<Entry<String, Object>> iterator = table.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Object> entry = iterator.next();
			if (entry.getKey().equals(TABLE_NAME_SYMBOL))
				continue;
			sql.eq(entry.getKey(), entry.getValue());
		}
		System.out.println(sql);
		System.out.println(DBTools.getDumpSql(db.query(sql)));
		return null;
	}

//	public void save(Hashtable<Serializable, Object> table) {
//		if (table.get(TABLE_NAME_SYMBOL) == null)
//			throw new DBException("the parameter must contains the key '" + TABLE_NAME_SYMBOL + "'");
//	}

}
